﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TTQT;
using TTQT.TTQT_Code;
using TTQT.TTQT_Code.Info;
using System.Data.SqlClient;
public partial class DoanhThu : MasterControl
{
    private int _dayIndex = 0;
    private int _day1Index = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!Page.IsPostBack)
        //{
        bindCN();
        bindGDV();
        txtTuNgay.Value = DateTime.Now.AddDays(-29).ToString("dd/MM/yyyy");
        txtDenNgay.Value = DateTime.Now.ToString("dd/MM/yyyy");
        //LoadDayList(true, DateTime.Now.Month, DateTime.Now.Year, DateTime.Now.Day);
        //}
    }
    private void bindCN()
    {
        string brnCode = "";
        dsCN.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
        dsCN.SelectCommand = "MaKho_GetAllForAdminCN";
        dsCN.SelectParameters.Clear();
        dsCN.SelectParameters.Add(new Parameter("makho"));
        dsCN.SelectParameters["makho"].DefaultValue = UserInfo.MaKho;
        try
        {
            cboCN.DataSourceID = "dsCN";
            cboCN.DataBind();
        }
        catch (Exception ex)
        {
            // Writing log methods here.
        }
        finally
        {
        }

    }

    private void bindGDV()
    {
        dsGDV.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
        dsGDV.SelectCommand = "User_getAllGDVByMakho";
        dsGDV.SelectParameters.Clear();
        dsGDV.SelectParameters.Add(new Parameter("maKho"));
        dsGDV.SelectParameters["maKho"].DefaultValue = cboCN.SelectedValue;

        dsGDV.SelectParameters.Add(new Parameter("group_name"));
        dsGDV.SelectParameters["group_name"].DefaultValue = "gdv";

        cboGDV.DataValueField = "user_name";
        cboGDV.DataTextField = "userNameAndFullName";
        cboGDV.DataSourceID = "dsGDV";

        cboGDV.DataBind();
        cboGDV.Items.Add(new ListItem("------------All------------", "-1"));
    }

    protected void btnXem_Click(object sender, EventArgs e)
    {

    }
    protected void btnThoat_Click(object sender, EventArgs e)
    {
        Response.Redirect(navigateUrl(0));
    }



    protected void btnTH_Click(object sender, EventArgs e)
    {
        //string fromDate = "", toDate = "";
        //DateTime f, t;
        //try
        //{

        //    f = Convert.ToDateTime(ddlYear.SelectedValue + "/" + ddlMonth.SelectedValue + "/" + ddlDay.SelectedValue);
        //    fromDate =
        //        TTQTCommon.convertDateString(ddlYear.SelectedValue + "/" + ddlMonth.SelectedValue + "/" +
        //                                     ddlDay.SelectedValue);

        //}
        //catch
        //{
        //    MyWebUtils.MyWebUtil.alert(this.Page, "Tu ngay khong hop le");
        //    return;
        //}
        //try
        //{
        //    t = Convert.ToDateTime(ddlYear1.SelectedValue + "/" + ddlMonth1.SelectedValue + "/" + ddlDay1.SelectedValue);
        //    t.AddHours(23);
        //    toDate =
        //       TTQTCommon.convertDateString(ddlYear1.SelectedValue + "/" + ddlMonth1.SelectedValue + "/" +
        //                                    ddlDay1.SelectedValue);
        //}
        //catch
        //{
        //    MyWebUtils.MyWebUtil.alert(this.Page, "Den ngay khong hop le");
        //    return;
        //}
        string store = "";
        int kq = -1;
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand();
        switch ("124")
        {
            case "BC":
                {
                    lblError.Visible = true;
                    lblError.Text = "Bạn phải chọn báo cáo để tổng hợp!";
                    return;
                }
            case "BC1":
                {
                    string makho = "000";
                    Session["MaKho"] = makho;
                    store = "_BM01_BaoCaoSeriACBiMat1New";
                    comm = new SqlCommand(store, conn);
                    comm.CommandType = CommandType.StoredProcedure;
                    comm.Parameters.AddWithValue("@UserId", Session["UserName"]);
                    comm.Parameters.AddWithValue("@MaKho", makho);
                    //comm.Parameters.AddWithValue("@TinhChatAC", ddlTinhChat.SelectedValue);
                    //comm.Parameters.AddWithValue("@LoaiAC", cboLoaiAnChi.SelectedValue);
                    //comm.Parameters.AddWithValue("@Dot", ddlDPH.SelectedValue);
                    //comm.Parameters.AddWithValue("@FromDate", Convert.ToDateTime(ddlYear.SelectedValue + "/" + ddlMonth.SelectedValue + "/" + ddlDay.SelectedValue));
                    //comm.Parameters.AddWithValue("@ToDate", Convert.ToDateTime(ddlYear1.SelectedValue + "/" + ddlMonth1.SelectedValue + "/" + ddlDay1.SelectedValue));
                    break;
                }

        }
        try
        {
            conn.Open();
            comm.Connection = conn;
            comm.ExecuteScalar();
            kq = 1;

            // Lay Ten CN
            comm.Parameters.Clear();
            comm.CommandType = CommandType.StoredProcedure;
            //if (ddlBaocao.SelectedValue.Equals("BC4") || ddlBaocao.SelectedValue.Equals("BC100"))
            //{
            //    comm.CommandText = "LayTenKho_TheoMa";
            //    comm.Parameters.AddWithValue("@makho", cboCN.SelectedValue);
            //}
            //else
            //{
            //    comm.CommandText = "LayTenChiNhanh_TheoTenDangNhap";
            //    comm.Parameters.AddWithValue("@User_name", UserInfo.userName);
            //}

            string tenCN = comm.ExecuteScalar().ToString();
            if (!string.IsNullOrEmpty(tenCN))
                Session["TenChiNhanh"] = tenCN;

        }
        catch (Exception ex)
        {
            kq = -1;
        }
        finally
        {
            conn.Close();
        }
        if (kq == -1)
        {
            lblError.Visible = true;
            lblError.Text = "Có lỗi tổng hợp báo cáo!";
            return;
        }
        else
        {
            lblError.Visible = true;
            lblError.Text = "Đã tổng hợp xong báo cáo!";
            return;
        }
    }


    protected void cboCN_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindGDV();
    }


}
